//------------------------------------------------------------------
//	hash table implementation
//------------------------------------------------------------------
#include <malloc.h>
#include <string.h>
#include "hashtable.h"

//------------------------------------------------------------------
//	HashTable
//	destructor
//------------------------------------------------------------------
HashTable::~HashTable()
{
	Clear(true,true);
}

//------------------------------------------------------------------
//	HashTable
//	set the number of buckets
//------------------------------------------------------------------
void HashTable::SetNBuckets( u32 nBuckets )
{
	if( m_pBuckets==0 )
	{
		m_pBuckets = new CHashList[nBuckets];
		m_nBuckets = nBuckets;
	}
}

//------------------------------------------------------------------
//	HashTable
//	clear the hash table out
//------------------------------------------------------------------
void HashTable::Clear( bool freeListMemory, bool freeBuckets )
{
	u32		i;
	for( i=0;i<m_nBuckets;i++ )
	{
		m_pBuckets[i].Clear(freeListMemory);
	}
	if( freeListMemory && freeBuckets )
	{
		delete[] m_pBuckets;
		m_pBuckets = 0;
		m_nBuckets = 0;
	}
}

//------------------------------------------------------------------
//	HashTable
//	add an item to the hashtable
//------------------------------------------------------------------
bool HashTable::AddByHash( HASH hashValue, void *pData )
{
	if( m_pBuckets==0 )
		return false;

	return m_pBuckets[hashValue%m_nBuckets].AddByHash(hashValue,pData);
}

//------------------------------------------------------------------
//	HashTable
//	remove an item from the hashtable
//------------------------------------------------------------------
bool HashTable::RemoveByHash( HASH hashValue )
{
	if( m_pBuckets==0 )
		return false;

	return m_pBuckets[hashValue%m_nBuckets].RemoveByHash(hashValue);
}
